package com.hw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.constants.ConstantCode;
import com.hw.dao.AreasDao;
import com.hw.exceptions.MyBaseException;
import com.hw.pojo.Areas;
import com.hw.service.AreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName: AreaServiceImpl
 * @Description:
 * @Author: vie0134
 * @Date: 2020-12-15 16:29
 * @Version: 1.0
 **/
@Service
public class AreasServiceImpl implements AreasService {
    @Autowired
    private AreasDao areasDao;

    @Override
    public PageInfo<Areas> findAllArea(int pageNum, int pageSize, String exludeId, String areaname, String status) {
        PageHelper.startPage(pageNum, pageSize);

        try {
            List<Areas> allArea = areasDao.findAllArea(exludeId, areaname, status);
//            allArea.forEach(areas -> {areas.setParentid(null);});
            return new PageInfo<>(allArea);
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MyBaseException("查询异常", ConstantCode.ERROR);
    }

    @Override
    public Areas findAreasById(Integer id) {
        if (id == null || id == 0) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            Areas area = areasDao.findAreasById(id);
            if (area == null) {
                throw new MyBaseException("没有查询此结果", ConstantCode.ERROR);
            }
            return area;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("查询失败", ConstantCode.ERROR);
        }
    }

    @Override
    public List<Areas> findAreasByParentId(Integer id) {
        if (id == null || id == 0) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            List<Areas> areasList = areasDao.findAreasByParentId(id);
            return areasList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("查询失败", ConstantCode.ERROR);
        }
    }

    @Override
    public List<Areas> findFirstParentAreas(String status) {

        try {
            return areasDao.findFirstParentAreas(status);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("查询失败", ConstantCode.ERROR);
        }
    }

    @Override
    public void addAreas(Areas areas) {
        if (areas == null) {
            throw new MyBaseException("数据不为空", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            areasDao.addAreas(areas);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("添加失败", ConstantCode.ERROR);
        }
    }

    @Override
    public void updateAreas(Areas areas) {
        if (areas == null || StringUtils.isEmpty(areas)) {
            throw new MyBaseException("数据不为空", ConstantCode.DATA_NOT_TRANSFER);
        }
        if ("0".equalsIgnoreCase(areas.getStatus())) {
            List<Areas> areasList = findAreasByParentId(areas.getId());
            if (areasList.size() != 0) {
                throw new MyBaseException("当前区域的子区域不能空，无法禁用", ConstantCode.DATA_NOT_TRANSFER);
            }
        }

        try {
            areasDao.updateAreas(areas);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("更新失败", ConstantCode.ERROR);
        }
    }

    @Override
    public void deleteAreasById(Integer id) {
        if (id == null) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            areasDao.deleteAreasById(id);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("删除失败", ConstantCode.ERROR);
        }
    }

    @Override
    public void deleteAreasByIds(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            areasDao.deleteAreasByIds(ids);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("删除失败", ConstantCode.ERROR);
        }
    }
}
